import React from 'react'
import { usePathname } from 'next/navigation'
import { useSelector } from 'react-redux'
import { RootState } from '@/store'
import { findAllMenuByPath } from '../../config/menu'
import PERM_ENUM from '@/permission/PermEnum'
import hasPerm from '@/permission/hasPerm'
import Forbidden from '@/app/forbidden'


const PermLayout: React.FC<
  Readonly<{
    children: React.ReactNode;
  }>
> = ({ children }) => {
  const pathname = usePathname();
  const loginUser = useSelector((state: RootState) => state.loginUser);
  const menu = findAllMenuByPath(pathname);
  const needPerm = menu?.needPerm ?? PERM_ENUM.NOT_LOGIN;
  const canAccess = hasPerm(loginUser, needPerm);
  if (!canAccess) {
    return <Forbidden />
  }
  return children;
};

export default PermLayout;
